
package com.axc.gallery2.net;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import com.renren.api.connect.android.Renren;
import com.renren.api.connect.android.Util;
import com.renren.api.connect.android.common.ResponseBean;
import com.renren.api.connect.android.photos.PhotoBean;

/**
 * photos.get API请求的返回结果包装类
 */
public class PhotosGetResponseBean extends ResponseBean implements Parcelable {

    private List<PhotoBean> photos = new ArrayList<PhotoBean>();

    /**
     * 字段常量
     */
    private static final String KEY_PID = "pid";
    private static final String KEY_AID = "aid";
    private static final String KEY_UID = "uid";
    private static final String KEY_URL_TINY = "url_tiny";
    private static final String KEY_URL_HEAD = "url_head";
    private static final String KEY_URL_LARGE = "url_large";
    private static final String KEY_CAPTION = "caption";
    private static final String KEY_TIME ="time";
    private static final String KEY_VIEW_COUNT="view_count";
    private static final String KEY_COMMENT_COUNT = "comment_count";
    


    /**
     * 构造函数，将请求返回的json串格式数据解析成对象
     * 
     * @param response
     */
    public PhotosGetResponseBean(String response) {
        this(response, Renren.RESPONSE_FORMAT_JSON);
    }

    /**
     * 构造函数，将请求返回的json串格式数据解析成对象
     * 
     * @param response 服务器返回的请求结果串
     * @param format 服务器返回结果的格式
     */
    public PhotosGetResponseBean(String response, String format) {
        super(response);
        if (response == null) {
            return;
        }
        
        SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd HH:mm:ss");

        // 对json格式数据进行解释
        if (format.toLowerCase().endsWith("json")) {
            //System.out.print(" 开始得到图片");
            try {
                JSONArray array = new JSONArray(response);
                int size = array.length();
                JSONObject object = null;
                for (int i = 0; i < size; i++) {
                    object = array.optJSONObject(i);
                    if (object != null) {
                        PhotoBean photo = new PhotoBean();
                        photo.setAid(object.optLong(KEY_AID));
                        photo.setPid(object.optLong(KEY_PID));
                        photo.setUid(object.optLong(KEY_UID));
                        photo.setUrlTiny(object.optString(KEY_URL_TINY));
                        photo.setUrlHead(object.optString(KEY_URL_HEAD));
                        photo.setUrlLarge(object.optString(KEY_URL_LARGE));
                        photo.setCaption(object.optString(KEY_CAPTION));
                        photo.setCreateTime(sdf.parse(object.optString(KEY_TIME)));
                        photo.setViewCount(object.optInt(KEY_VIEW_COUNT));
                        photo.setCommentCount(object.getInt(KEY_COMMENT_COUNT));
                        
                        photos.add(photo);
                        //System.out.print(" 第"+ i +"张图片");
                    }
                }
            } catch (JSONException e) {
                Util.logger("exception in parsing json data:" + e.getMessage());
            } catch (ParseException e) {
                Util.logger("exception in parsing json data: " + e.getMessage());
            }
        }
    }
    
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(photos);
    }

    public List<PhotoBean> getPhotos() {
        return photos;
    }

    public void setPhotos(List<PhotoBean> photos) {
        if (photos != null) {
            this.photos = photos;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();

        for (PhotoBean photo : photos) {
            sb.append("\n").append(photo.toString()).append("\r\n");
        }

        return sb.toString();
    }
    
    public static final Parcelable.Creator<PhotosGetResponseBean> CREATOR = new Parcelable.Creator<PhotosGetResponseBean>() {
        public PhotosGetResponseBean createFromParcel(Parcel in) {
            return new PhotosGetResponseBean(in);
        }

        public PhotosGetResponseBean[] newArray(int size) {
            return new PhotosGetResponseBean[size];
        }
    };
    
    /**
     * 序列化构造函数
     * 
     * @param in
     */
    public PhotosGetResponseBean(Parcel in) {
        super(null);

        in.readTypedList(photos, PhotoBean.CREATOR);
    }
}
